﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using Fuse8.ContentManagementFramework.Domain;
using Fuse8.DomainFramework.DataAccess.SqlDatabase;

namespace Fuse8.ContentManagementFramework.DataAccess.SqlAccess.Mappers
{
    public class CmsEntityMetaFieldLinkDataMapper : BaseSqlEntityDataMapper<CmsEntityMetaFieldLink, int>
    {
        private Dictionary<string, Func<CmsEntityMetaFieldLink, SqlDataReader, CmsEntityMetaFieldLink>>  specialMapsFrom
            = new Dictionary<string, Func<CmsEntityMetaFieldLink, SqlDataReader, CmsEntityMetaFieldLink>>()
                  {
                      {"cms_sp_EntityMetaFieldLinkSelectForType", MapForEntityType},
                      {"cms_sp_EntityMetaFieldLinkSelectOne", MapForEntityType}
                  };

        public CmsEntityMetaFieldLinkDataMapper()
            : base(new[] { "EntityMetaField", "IsLast", "IsFirst" })
        {
        }

        public override CmsEntityMetaFieldLink MapFrom(string sp, System.Data.SqlClient.SqlDataReader reader)
        {
            var entity = base.MapFrom(sp, reader);

            return specialMapsFrom.ContainsKey(sp) ? specialMapsFrom[sp](entity, reader) : entity;
        }

        private static CmsEntityMetaFieldLink MapForEntityType(CmsEntityMetaFieldLink entity, SqlDataReader reader)
        {
            entity.EntityMetaField = new CmsEntityMetaField
                                         {
                                             Id =
                                                 reader.GetInt32(reader.GetOrdinal("EntityMetaField_EntityMetaFieldId")),
                                             MetaFieldName =
                                                 reader.GetString(reader.GetOrdinal("EntityMetaField_MetaFieldName")),
                                             MetaFieldType =
                                                 reader.GetString(reader.GetOrdinal("EntityMetaField_MetaFieldType")),
                                             MetaFieldDescription = Convert.ToString(reader["EntityMetaField_MetaFieldDescription"]),
                                             IsSystem = reader.GetBoolean(reader.GetOrdinal("EntityMetaField_IsSystem"))
                                         };


            return entity;
        }
    }
}